package repository

import "gitee.com/anxu/golang-web/src/models"

var Thread = new(thread)

type thread struct{}

func (t *thread) FindAll() []models.Thread {
	var threads []models.Thread
	models.Db.Preload("User").Find(&threads)
	return threads
}

func (t *thread) FindByUuid(uuid string) models.Thread {
	var th models.Thread
	models.Db.Preload("Posts").Preload("Posts.User").Preload("User").Where("uuid = ?", uuid).First(&th)
	return th
}

func (t *thread) FindById(id int) models.Thread {
	var thread models.Thread
	models.Db.First(thread, id)
	return thread
}

func (t *thread) Create(thread *models.Thread) *models.Thread {
	res := models.Db.Create(thread)
	if res.RowsAffected > 0 {
		return thread
	}
	return nil
}
